﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace Widgetsphere.Autos.Websitev2.Objects
{
    public class BasePage : System.Web.UI.Page
    {
        protected string _metaTagDescription = "";
        protected string _metaTagKeywords = "";

        public MasterPageBase MyMaster
        {
            get { return (MasterPageBase) this.Master; }
        }

        /// <summary>
        /// Allows for the setting of the Description meta tag
        /// </summary>
        public string MetaTagDescription
        {
            get { return _metaTagDescription; }
            set { _metaTagDescription = value; }
        }

        /// <summary>
        /// Allows for the setting of the Keywords meta tag
        /// </summary>
        public string MetaTagKeywords
        {
            get { return _metaTagKeywords; }
            set { _metaTagKeywords = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            Trace.Write("BasePage OnPreRender BEGIN");

            try
            {
                base.OnPreRender(e);

                //These literals are there make the HTML pretty
                var l = new LiteralControl();
                l.Text = "\n";
                this.Page.Header.Controls.AddAt(0, l);

                HtmlMeta newTag = null;

                //newTag = new HtmlMeta();
                //newTag.Attributes.Add("http-equiv", "cache-control");
                //newTag.Attributes.Add("content", "no-cache");
                //this.Page.Header.Controls.AddAt(0, newTag);

                l = new LiteralControl();
                l.Text = "\n";
                this.Page.Header.Controls.AddAt(0, l);

                //Load all the meta tags
                //HtmlMeta[] metaTags = GetCurrentMetaTags();
                var metaTags = new HtmlMeta[0];
                var tagNameList = new List<string>();
                foreach (var tag in metaTags)
                {
                    tagNameList.Add(tag.Name.ToLower());
                    this.Page.Header.Controls.AddAt(0, tag);

                    l = new LiteralControl();
                    l.Text = "\n";
                    this.Page.Header.Controls.AddAt(0, l);
                }

                //If there is no keywords then use default
                if (!tagNameList.Contains("keywords"))
                {
                    //There is nothing in the document so use the default
                    newTag = new HtmlMeta();
                    newTag.Attributes.Add("name", "keywords");
                    newTag.Attributes.Add("content", this.MetaTagKeywords);
                    this.Page.Header.Controls.AddAt(0, newTag);

                    l = new LiteralControl();
                    l.Text = "\n";
                    this.Page.Header.Controls.AddAt(0, l);
                }

                //If there is no description then use default
                if (!tagNameList.Contains("description"))
                {
                    newTag = new HtmlMeta();
                    newTag.Attributes.Add("name", "description");
                    newTag.Attributes.Add("content", this.MetaTagDescription);
                    this.Page.Header.Controls.AddAt(0, newTag);

                    l = new LiteralControl();
                    l.Text = "\n";
                    this.Page.Header.Controls.AddAt(0, l);
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            Trace.Write("BasePage OnPreRender END");

        }

        protected override void Render(HtmlTextWriter writer)
        {
            Trace.Write("BasePage Render BEGIN");
            var debugInt = 0;
            try
            {
                var stringWriter = new System.IO.StringWriter();
                debugInt = 1;
                var htmlWriter = new HtmlTextWriter(stringWriter);
                debugInt = 2;
                base.Render(htmlWriter);
                debugInt = 3;

                var html = stringWriter.ToString();
                debugInt = 4;

                writer.Write(html);
                debugInt = 17;
            }
            catch (Exception ex)
            {
                //TODO Send Mail
                throw;
            }

            Trace.Write("BasePage Render END");

        }

    }
}